//
//  TagTableViewController.m
//  xxShare
//
//  Created by 柳麟喆 on 2018/4/1.
//  Copyright © 2018年 lzLiu. All rights reserved.
//

#import "TagTableViewController.h"

@interface TagTableViewController ()

@property (nonatomic, copy)NSArray *array;
@property (nonatomic, copy)NSMutableArray *choosedTag;

@end

@implementation TagTableViewController

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BACKGROUND_COLOR;
    self.navigationItem.title = @"选择标签";
    _array = @[@"美食",@"风景",@"萌宠",@"动漫",@"军事"];
    _choosedTag = [NSMutableArray arrayWithObjects:@"0",@"0",@"0",@"0",@"0", nil];
    //添加保存按钮
    UIBarButtonItem *saveBtn = [[UIBarButtonItem alloc]initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveTag)];
    self.navigationItem.rightBarButtonItem = saveBtn;
    
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"CELL";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    cell.backgroundColor = BACKGROUND_COLOR;
    cell.textLabel.text = _array[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = NO;
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        cell.accessoryType = UITableViewCellAccessoryNone;
        _choosedTag[indexPath.row] = @"0";
    }else{
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        _choosedTag[indexPath.row] = @"1";
    }
}

#pragma mark - *******私有方法*******
- (void)saveTag{
    NSMutableArray *tagArray = [NSMutableArray array];
    for (int i = 0; i < 5; i++) {
        if ([_choosedTag[i] isEqualToString:@"1"]) {//被选中的索引
            [tagArray addObject:_array[i]];
        }
    }
    [[NSNotificationCenter defaultCenter]postNotificationName:@"tagNotification" object:tagArray];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
